package org.edu.modules.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.SecurityUtils;
import org.edu.common.system.vo.LoginUser;
import org.edu.modules.oa.entity.VeOaScheduleEntity;
import org.edu.modules.oa.mapper.IVeOaSchedulExcleVoMapper;
import org.edu.modules.oa.mapper.IVeOaScheduleMapper;
import org.edu.modules.oa.service.IVeOaScheduleService;
import org.edu.modules.oa.vo.VeOaScheduleExcleVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class IVeOaScheduleServiceImpl implements IVeOaScheduleService {
    @Resource
    private IVeOaScheduleMapper iVeOaScheduleMapper;

    @Resource
    private IVeOaSchedulExcleVoMapper iVeOaSchedulExcleVoMapper;
    //查询我的日程安排(分页、名字模糊查询)
    @Override
    public HashMap<String, Object> showMySchedule(Integer nowPage, Integer pageSize, VeOaScheduleEntity veOaScheduleEntity) {
        //获取redis里缓存的用户
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        HashMap<String, Object> map = new HashMap<>();
        QueryWrapper<VeOaScheduleEntity> wrapper = new QueryWrapper();
        //分页插件
        IPage<VeOaScheduleEntity> page = new Page<>(nowPage, pageSize);
        if (null != veOaScheduleEntity.getTitle()) {
            wrapper.like("title", veOaScheduleEntity.getTitle());
        }

        wrapper.eq("IS_DELETED", 0);
        wrapper.eq("create_user", user.getId());
        wrapper.orderByDesc("create_time");
        //获取集合
        page = iVeOaScheduleMapper.selectPage(page, wrapper);
        List<VeOaScheduleEntity> list = page.getRecords();
        //获取个数
        Integer count = iVeOaScheduleMapper.selectCount(wrapper);

        map.put("list", list);
        map.put("count", count);
        return map;
    }

    //删除一个日程(逻辑删除)
    @Override
    public Integer deleteOneSchedule(Integer id) {
        Integer num = iVeOaScheduleMapper.deleteById(id);
        return num;
    }

    //删除多条记录
    @Override
    public Integer deleteMoreSchedule(List<Integer> list) {
        Integer num = iVeOaScheduleMapper.deleteBatchIds(list);
        return num;
    }

    //事务类型的集合
    @Override
    public List<VeOaScheduleEntity> showTitleTypeList() {
        QueryWrapper<VeOaScheduleEntity> wrapper = new QueryWrapper();
        wrapper.select("title_type");
        //获取redis里缓存的用户
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        wrapper.eq("CREATE_USER",user.getId());
        wrapper.groupBy("title_type");
        List<VeOaScheduleEntity> list = iVeOaScheduleMapper.selectList(wrapper);
        return list;
    }

    //插入一条日程安排
    @Override
    public Integer insertSchedule(VeOaScheduleEntity veOaScheduleEntity,String start,String end,String remind){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date startTime = format.parse(start);
            Date endTime = format.parse(end);
            Date remindTime = format.parse(remind);
            veOaScheduleEntity.setStartTime(startTime);
            veOaScheduleEntity.setEndTime(endTime);
            veOaScheduleEntity.setRemindTime(remindTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        veOaScheduleEntity.setState("待处理");
        //获取redis里缓存的用户
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        veOaScheduleEntity.setCreateUser(user.getId());
        veOaScheduleEntity.setCreateUsername(user.getRealname());
        veOaScheduleEntity.setIsDeleted(0);
        veOaScheduleEntity.setTerminalId(1);
        Integer num = iVeOaScheduleMapper.insert(veOaScheduleEntity);
        return num;
    }
    //修改状态
    @Override
    public Integer updateState(VeOaScheduleEntity veOaScheduleEntity) {
        Integer num = iVeOaScheduleMapper.updateById(veOaScheduleEntity);
        return num;
    }

    //修改日程信息
    @Override
    public Integer updateInfo(VeOaScheduleEntity veOaScheduleEntity,String start,String end,String remind) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date startTime = format.parse(start);
            Date endTime = format.parse(end);
            Date remindTime = format.parse(remind);
            veOaScheduleEntity.setStartTime(startTime);
            veOaScheduleEntity.setEndTime(endTime);
            veOaScheduleEntity.setRemindTime(remindTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Integer num = iVeOaScheduleMapper.updateById(veOaScheduleEntity);
        return num;
    }

    //要导出文件的集合
    @Override
    public List<VeOaScheduleExcleVo> selectAllMySchedule() {
        //获取redis里缓存的用户
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        QueryWrapper<VeOaScheduleExcleVo> wrapper = new QueryWrapper();
        wrapper.eq("create_user",user.getId());
        wrapper.eq("IS_DELETED",0);
        List<VeOaScheduleExcleVo> list = iVeOaSchedulExcleVoMapper.selectList(wrapper);
        return list;
    }

    //获取日历上这一个月的集合
    @Override
    public HashMap<String, Object>  selectDsplayData(VeOaScheduleEntity veOaScheduleEntity,String start,String end) {
        HashMap<String, Object> map = new HashMap<>();
        //获取redis里缓存的用户
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        QueryWrapper<VeOaScheduleEntity> wrapper = new QueryWrapper();
        if (null != veOaScheduleEntity.getTitle()) {
            wrapper.like("title", veOaScheduleEntity.getTitle());
        }
        wrapper.eq("create_user",user.getId());
        wrapper.eq("IS_DELETED",0);

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date startTime = format.parse(start);
            Date endTime = format.parse(end);
            wrapper.ge("start_time",startTime);
            wrapper.lt("start_time",endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        List<VeOaScheduleEntity> list=iVeOaScheduleMapper.selectList(wrapper);
        map.put("list",list);
        return map;
    }

    @Override
    public HashMap<String, Object> showMySchedules(String userId,String startTime,String endTime) {

        HashMap<String, Object> map = new HashMap<>();
        QueryWrapper<VeOaScheduleEntity> wrapper = new QueryWrapper();
        wrapper.eq("IS_DELETED", 0);
        wrapper.eq("create_user", userId);
        wrapper.orderByDesc("create_time");

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date startTime1 = format.parse(startTime);
            Date endTime1 = format.parse(endTime);
            wrapper.ge("start_time",startTime1);
            wrapper.lt("start_time",endTime1);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //获取集合
        List<VeOaScheduleEntity> list = iVeOaScheduleMapper.selectList(wrapper);
        //获取个数
        Integer count = iVeOaScheduleMapper.selectCount(wrapper);
        map.put("list", list);
        map.put("count", count);
        return map;
    }

}
